package com.dcloud.streamweb.uploadfile.controller;

import java.io.*;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import java.net.URLDecoder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.streamweb.constants.SystemConstants;
import com.dcloud.streamweb.sysconfig.service.ISystemConfigService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.dcloud.streamweb.uploadfile.entity.UploadFile;
import com.dcloud.streamweb.uploadfile.service.IUploadFileService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;



 /**
 * @Description: upload_file
 * @Author: jeecg-boot
 * @Date:   2024-07-29
 * @Version: V1.0
 */
@Api(tags="jar文件管理")
@RestController
@RequestMapping("/api/uploadfile/v1")
@Slf4j
public class UploadFileController extends JeecgController<UploadFile, IUploadFileService> {
	@Autowired
	private IUploadFileService uploadFileService;

	 @Autowired
	 private ISystemConfigService systemConfigService;
	
	/**
	 * 分页列表查询
	 *
	 * @param uploadFile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "upload_file-分页列表查询")
	@ApiOperation(value="upload_file-分页列表查询", notes="upload_file-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<UploadFile>> queryPageList(UploadFile uploadFile,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UploadFile> queryWrapper = QueryGenerator.initQueryWrapper(uploadFile, req.getParameterMap());
		Page<UploadFile> page = new Page<UploadFile>(pageNo, pageSize);
		IPage<UploadFile> pageList = uploadFileService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param uploadFile
	 * @return
	 */
	@AutoLog(value = "upload_file-添加")
	@ApiOperation(value="upload_file-添加", notes="upload_file-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody UploadFile uploadFile) {
		if(uploadFile.getType() == null){
			return Result.error("添加失败，必传参数 文件类型");
		}

		//如果是udf  必传主方法入口，udf自定义方法名称，udf使用案例说明
		if(uploadFile.getType() == 2 && (uploadFile.getUdfMethodName() == null || uploadFile.getUdfUseCase() == null)){
			return Result.error("添加失败，文件类型,必须udf自定义方法名称，udf使用案例说明");
		}

		uploadFileService.save(uploadFile);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param uploadFile
	 * @return
	 */
	@AutoLog(value = "upload_file-编辑")
	@ApiOperation(value="upload_file-编辑", notes="upload_file-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody UploadFile uploadFile) {
		if(uploadFile.getType() == null){
			return Result.error("添加失败，必传参数 文件类型");
		}

		//如果是udf  必传主方法入口，udf自定义方法名称，udf使用案例说明
		if(uploadFile.getType() == 2 && (uploadFile.getUdfMethodName() == null || uploadFile.getUdfUseCase() == null)){
			return Result.error("添加失败，文件类型,必须udf自定义方法名称，udf使用案例说明");
		}
		uploadFileService.updateById(uploadFile);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "upload_file-通过id删除")
	@ApiOperation(value="upload_file-通过id删除", notes="upload_file-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		uploadFileService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "upload_file-批量删除")
	@ApiOperation(value="upload_file-批量删除", notes="upload_file-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.uploadFileService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "upload_file-通过id查询")
	@ApiOperation(value="upload_file-通过id查询", notes="upload_file-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<UploadFile> queryById(@RequestParam(name="id",required=true) String id) {
		UploadFile uploadFile = uploadFileService.getById(id);
		if(uploadFile==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(uploadFile);
	}

	 public static void main(String[] args) {
		 try {
			 InetAddress address = InetAddress.getLocalHost();
			 String ip = address.getHostAddress();
			 System.out.println("当前服务器的IP地址是：" + ip);
		 } catch (UnknownHostException e) {
			 e.printStackTrace();
		 }
	 }

}
